import Vue from 'vue'
import Vuex from 'vuex'
import http from '../api'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		userCartNum: '0', // 购物车商品数量
		confirmAddress: '', // 确认订单修改收货地址
		userInfo: {},
		isLogin: false,
		webConfig:[]
	},
	getters: {
		getuserCartNum(state) {
			return state.userCartNum
		},
		getconfirmAddress(state) {
			return state.confirmAddress
		},
		getuserInfo(state) {
			return state.userInfo
		},
		showMutipleShopper(state){
			const target = state.webConfig.find(res=> res.ename==='open_many_shop') || {}
			return target.value==='1' || false
		},
	},
	mutations: {
		/**
		 *
		 * 修改购物车商品数量
		 * @param {any} state
		 * @param {any} data
		 */
		CartNum(state, data) {
			state.userCartNum = data
		},
		/**
		 *  确认订单修改收货地址
		 */
		editAddress(state, data = '') {
			state.confirmAddress = data
		},
		// 存储用户信息
		setUserInfo(state, data = {}) {
			state.userInfo = data
		},
		loginStatus(state, isLogin = false) {
			state.isLogin = isLogin
		},
		setWebConfig(state,data) {
			state.webConfig = data
		}
	},

	actions: {
		getShowConfig({commit},data){
			http.getShowConfig().then(res=>{
				if(res.status=200){
					commit('setWebConfig',res.data)
				}
			})
		},
		contentnum({commit}, data) {
			http.getgetnum(data).then(res => {
				if (res.status == 200) {
					commit('CartNum', res.data.countnum)
					
					if (Number(res.data.countnum) > 0) {
						uni.setTabBarBadge({
							index: 3,
							text: `${res.data.countnum}`
						})
					} else {
						uni.removeTabBarBadge({
							index: 3,
						})
					}
				}
			})

		}
	}
})

export default store
